<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>和为s的两个数字</title>
</head>
<body>
  <script>
    function fn(nums, target) {
      let [left, right] = [0, nums.length - 1];
      while (left < right) {
        sum = nums[left] + nums[right];
        if (sum > target) {
          // 太大了，右指针左移
          right--;
        } else if (sum < target) {
          // 太小了，左指针右移
          left++;
        } else {
          // 找到，直接返回两个数
          return [nums[left], nums[right]];
        }
      }
      return []
    }
    console.log(fn([2,7,11,15], 9))
  </script>
</body>
</html>